/* Copyright (c) 2011-2013 ISP RAS, Moscow. For more information please write to
 
      mermakov@ispras.ru
      svartanov@ispras.ru

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package ru.ispras.coffeemachine.instrument;

import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.DUP2_X1;
import org.apache.bcel.generic.DUP_X2;
import org.apache.bcel.generic.InstructionList;

import ru.ispras.coffeemachine.target.Type;

/**
 * Combined STP trace and TSan trace instrumentation
 * 
 * @author Mikhail Ermakov
 * @version 1.2.0
 * @since 2013-01-09
 */

public class CombinedMilk extends DaemonMilk {

	private final ConcurrencyMilk cMilk;
	
	public CombinedMilk(ClassGen targetClass, ConstantPoolGen cpg) {

		super(targetClass, cpg);
		cMilk = new ConcurrencyMilk(targetClass, cpg);
	}
	
	@Override
	public void setCurrentMethod(CoffeeMethodGen currentMethod) {

		super.setCurrentMethod(currentMethod);
		cMilk.setCurrentMethod(currentMethod);
	}
	
	@Override
	public void athrow() {

		cMilk.isLastInstruction = isLastInstruction;
		cMilk.athrow();
		instructionList.append(cMilk.instructionList);
	}
	
	@Override
	protected InstructionList createFirstList()
	{
		InstructionList result = new InstructionList();
		InstructionList daemonResult = super.createFirstList();
		InstructionList concResult = cMilk.createFirstList();
		if (daemonResult != null) {
			result.append(daemonResult);
		}
		if (concResult != null) {
			result.append(concResult);
		}
		if (result.size() == 0) {
			return null;
		}
		return result;
	}
	
	/*
	 * Instructions
	 */
	
	@Override
	protected void xaload(Type type)
	{
		super.xaload(type);
		cMilk.xaload(type);
		instructionList.append(cMilk.instructionList);
	}
	
	@Override
	protected void xastore(Type type)
	{
		removeInstruction = true;
		String storeType;
		String shortType;
		if (type.isScalar()) {
			storeType = type.toString();
			shortType = storeType;
		} else {
			storeType = "Ljava/lang/Object;";
			shortType = "R";
		}
		if (type.equals(Type.B) || type.equals(Type.Z)) {
			removeInstruction = false;
			instructionList.append(new DUP_X2());
			appendMethod("xastoreHelper", "(" + storeType + ")V");
			instructionList.append(new DUP2_X1());
			updateCodePosition();
			appendMethod("xastore" + shortType, "(Ljava/lang/Object;II)V");
		} else {
			updateCodePosition();
			appendMethod("xastore" + shortType + "Direct", "([" + storeType + "I" + storeType + "I)V");
		}
	}

	@Override
	protected void xreturn(boolean popStack)
	{
		super.xreturn(popStack);
		cMilk.xreturn(popStack);
		instructionList.append(cMilk.instructionList);
	}

	@Override
	protected void getstatic(Object[] arguments)
	{
		super.getstatic(arguments);
		cMilk.getstatic(arguments);
		instructionList.append(cMilk.instructionList);
	}

	@Override
	protected void putstatic(Object[] arguments) {
		
		super.putstatic(arguments);
		cMilk.putstatic(arguments);
		instructionList.append(cMilk.instructionList);
	}

	@Override
	protected void getfield(Object[] arguments)
	{
		super.getfield(arguments);
		cMilk.getfield(arguments);
		instructionList.append(cMilk.instructionList);
	}

	@Override
	protected void putfield(Object[] arguments)
	{
		super.putfield(arguments);
		cMilk.putfield(arguments);
		instructionList.append(cMilk.instructionList);
	}

	@Override
	protected void invoke(Object []parameters, short type)
	{
		super.invoke(parameters, type);
		cMilk.invoke(parameters, type);
		instructionList.append(cMilk.instructionList);
	}
	
	@Override
	protected void monitor(boolean isEnter)
	{
		super.monitor(isEnter);
		cMilk.monitor(isEnter);
		instructionList.append(cMilk.instructionList);
	}
	
	/*
	 * After instructions
	 */
	
	@Override
	protected void invokeAfter(Object []parameters, short type)
	{
		cMilk.invokeAfter(parameters, type);
		instructionList.append(cMilk.instructionList);
		super.invokeAfter(parameters, type);
	}
	
	@Override
	protected void monitorAfter(boolean isEnter)
	{
		cMilk.monitorAfter(isEnter);
		instructionList.append(cMilk.instructionList);
	}
	
	

}
